package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyKpNdkpjh;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyKpNdkpjhMapper;
import com.yuncheng.spcyApi.service.ISpcyKpNdkpjhService;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 检查员-年度考评计划
 * @Author: yuncheng
 * @Date: 2024-02-03
 * @Version: V1.0
 */
@Service
public class SpcyKpNdkpjhServiceImpl extends BaseServiceImpl<SpcyKpNdkpjhMapper, SpcyKpNdkpjh> implements ISpcyKpNdkpjhService {

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public SpcyKpNdkpjh addNdjh(SpcyKpNdkpjh bean) {
        if (StringUtils.isBlank(bean.getFkpnd())){
            bean.setFkpnd(DateConversionUtils.DateToYear(new Date()));
        }

        /** 验证年度是否存在 */
        List<SpcyKpNdkpjh> ndList = list(new LambdaQueryWrapper<SpcyKpNdkpjh>()
                .eq(SpcyKpNdkpjh::getFkpnd, bean.getFkpnd()));
        if (CollectionUtil.isNotEmpty(ndList)){
            throw new BusinessException("已经存在【"+bean.getFkpnd()+"】的年度考核了！");
        }

        if (StringUtils.isBlank(bean.getId())){
            bean.setId(GetUuIdUtils.ReplaceUuId());
        }

        bean.setFstate("未开始");

        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        bean.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        save(bean);

        String operContent = "新增信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_JCY_NDKPJH,operContent,bean.getId(), "添加年度考评计划", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    @Override
    public void updateNdjh(SpcyKpNdkpjh buffer) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String id = buffer.getId();

        SpcyKpNdkpjh updateBean = getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(buffer));

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());

        /** 验证年度是否存在 */
        if (StringUtils.isNotBlank(updateBean.getFkpnd())) {
            List<SpcyKpNdkpjh> ndList = list(new LambdaQueryWrapper<SpcyKpNdkpjh>()
                    .eq(SpcyKpNdkpjh::getFkpnd, updateBean.getFkpnd()));
            if (CollectionUtil.isNotEmpty(ndList)) {
                if (ndList.size() == 1){
                    if (ndList.get(0).getId().equals(id)){

                    }else {
                        throw new BusinessException("已经存在【" + updateBean.getFkpnd() + "】的年度考核了！");
                    }
                }else {
                    throw new BusinessException("已经存在【" + updateBean.getFkpnd() + "】的年度考核了！");
                }
            }
        }

        updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_JCY_NDKPJH,operContent,id,"修改年度考评计划", SpcyConstant.LOGS_SYS_TYPE_XG);

    }

    @Override
    public void deleteNdjh(String id) {
        SpcyKpNdkpjh bean = getById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        if (StringUtils.isNotBlank(bean.getFstate()) && bean.getFstate().equals("已开始")){
            throw new BusinessException("计划已开始，无法删除！");
        }

        removeById(id);
    }
}
